terminology = {}
for row in file("NICNOC", "r"):
    tid, did, term = row.strip().split("\t")
    terminology[did] = term

tmp_rank = []
for row in file("Corpus_NICNOC", "r"):
    did, freq = row.strip().split("\t")
    term = terminology[did]
    tmp_rank.append((int(freq,10), term, did))
    tmp_rank.sort()
    tmp_rank.reverse()

for (f, t, d) in tmp_rank:
    print "%s\t%s\t%s" %(d, t, f)
